import api from '../config/api'

import { globalData } from '../config/globalData'
import Ajax from '../utils/ajax'
import baseUrl from '../baseUrl'

export default class Product {
  /**
   * 获取商品详情
   */
  static async getGoodsDetail (param) {
    let url = baseUrl.server + api.GET_GOODS_DETAIL
    param.token = globalData.access_token
    let res = await Ajax.get(url, param)
    return res
  }

  /**
   * 获取商品主图
   */
  static async getGoodsImages (id) {
    let url = baseUrl.server + api.GET_GOODS_IMAGES
    let param = {
      token: globalData.access_token,
      goods_sku: id
    }
    let res = await Ajax.get(url, param)
    return res
  }

  /**
   * 获取商品库存
   */
  static async getGoodsStocks (param) {
    let url = baseUrl.server + api.GET_GOODS_STOCKS
    let dfparam = {
      token: globalData.access_token
    }
    param = Object.assign(dfparam, param)
    let res = await Ajax.get(url, param)
    return res
  }

  /**
   * 获取折扣值列表
   */
  static async getDiscountValueList () {
    let url = baseUrl.server + api.GET_DISCOUNT_VALUE_LIST
    let res = await Ajax.post(url, {
      token: globalData.access_token
    })
    return res
  }

  /**
   * 获取折扣产品列表
   */
  static async getDiscountProductList (param) {
    let url = baseUrl.server + api.GET_DISCOUNT_PRODUCT_LIST
    param.token = globalData.access_token
    let res = await Ajax.post(url, param)
    return res
  }

  /**
   * 获取分享产品列表
   */
  static async getDistributionProductList (param) {
    let url = baseUrl.server + api.GET_DISTRIBUTION_PRODUCT_LIST
    param.token = globalData.access_token
    let res = await Ajax.post(url, param)
    return res
  }

  /**
   * 获取折扣产品详情
   */
  static async getDiscountProductInfo (param) {
    let url = baseUrl.server + api.GET_DISCOUNT_PRODUCT_INFO
    param.token = globalData.access_token
    let res = await Ajax.post(url, param)
    return res
  }

  /**
   * 获取折扣商品图片列表id
   */
  static async getDiscountProductImages (id) {
    let url = baseUrl.server + api.GET_DISCOUNT_PRODUCT_IMAGES
    let param = {
      token: globalData.access_token,
      goods_sku: id
    }
    let res = await Ajax.post(url, param)
    return res
  }

  /**
   * 加入购物车
   */
  static async addToCart (id, goods_num = 1) {
    let url = baseUrl.server + api.ADD_TO_CART
    let param = {
      token: globalData.access_token,
      goods_sku: id,
      goods_num: goods_num
    }
    let res = await Ajax.post(url, param)
    return res
  }

  /**
   * 加入购物车
   */
  static async editToCart (param = { cart_id: null, quantity: 1 }) {
    let url = baseUrl.server + api.EDIT_TO_CART
    let dfparam = {
      token: globalData.access_token
    }
    param = Object.assign(dfparam, param)
    let res = await Ajax.post(url, param)
    return res
  }

  /**
   * 加入购物车
   */
  static async getGoodsAdditionalInfo (param) {
    let url = baseUrl.server + api.GOODS_ADDITIONAL_INFO
    let dfparam = {
      token: globalData.access_token
    }
    param = Object.assign(dfparam, param)
    let res = await Ajax.post(url, param)
    return res
  }

  /**
   * 获取新折扣列表
   */
  static async getNewDiscountList (param) {
    let url = baseUrl.server + api.NEW_DISCOUNT
    let dfparam = {
      token: globalData.access_token
    }
    param = Object.assign(dfparam, param)
    let res = await Ajax.get(url, param)
    return res
  }
}
